package programs.kind;

import game.SpaceObject;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import programs.expression.superClasses.ExpressionType;

public abstract class ForEachType {
		
	public abstract List<SpaceObject> getAll(game.Ship executer);
	
	public int getNbOfObjects(game.Ship executer) {
		return getAll(executer).size();
	}
	
	public ExpressionType getReturnType() {
		return ExpressionType.ENTITY;
	}

	public Iterator<SpaceObject> getIterator(final game.Ship executer) {
		return new Iterator<SpaceObject>() {
			
			private int indexOfLastReturnedObject = 0;	

			@Override
			public boolean hasNext() {
				return indexOfLastReturnedObject < getNbOfObjects(executer);
			}

			@Override
			public SpaceObject next() {
				if(!hasNext())
					throw new NoSuchElementException();
				indexOfLastReturnedObject++;
				return getAll(executer).get(indexOfLastReturnedObject - 1);
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
			
		};
	}
}
